<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>点击我切换a</button>
    <button>点击我切换b</button>
    <div class="page"></div>
    <script>
        var pageEle = document.querySelector(".page");
        var btn1 = document.querySelectorAll("button")[0];
        var btn2 = document.querySelectorAll("button")[1];

        // 点击按钮的时候设置不同的哈希值；
        btn1.onclick = function () {
            window.location.hash = "page1";
            renderDom()
        }

        btn2.onclick = function () {
            window.location.hash = "page2";
            renderDom()
        }

        function renderDom() {
            // 获取 页面的哈希值；
            var hashStr = window.location.hash;
            switch (hashStr) {
                case '#page1':
                    pageEle.innerHTML = "<h1>我是a页面</h1>";
                    break;
                case '#page2':
                    pageEle.innerHTML = "<h1>我是b页面</h1>";
                    break;
                default:
                    console.log("输入错误")
                    break;
            }
        }


    </script>
</body>
